<?php

global $_W, $_GPC;

$ops = array('list', 'category', 'category_add', 'category_del', 'news_post', 'news_delete', 'news_rec', 'news_show', 'news_detail');
$op  = in_array($_GPC['op'], $ops) ? $_GPC['op'] : 'list';
//var_dump($op);

if ($op == 'list') {
    $query      = load()->object('query');
    $categorys  = $query->from('dgjy_article_category')->getall();//动态列表的分类
    $categorys2 = array();//动态列表条目展示用的分类
    foreach ($categorys as $key) {
        $categorys2[$key['id']] = $key;
    }

    $pindex = max(1, intval($_GPC['page']));
    $psize  = 10;

    $cateid = intval($_GPC['cateid']);
    $title  = safe_gpc_string($_GPC['title']);

    $sql = "SELECT * FROM " . tablename('dgjy_article_news');

    if (!empty($cateid)) {
        $sql = $sql . "where cateid = $cateid ";
    }

    if (!empty($title)) {
        $sql = $sql . "where title like '%" . $title . "%' ";
    }

    $order = !empty($_GPC['setting']) ? $_GPC['setting'] : 'createtime desc';

    $sql = $sql . " ORDER BY " . $order;

    $newsall = pdo_fetchall($sql);
    $total   = (count($newsall));
    $pager   = pagination($total, $pindex, $psize);

    $sql = $sql . " LIMIT " . ($pindex - 1) * $psize . "," . $psize;
//    var_dump($sql);
    $news = pdo_fetchall($sql);

    include $this->template('news/news');
}

if ($op == 'category') {
    $_W['page']['title'] = '动态分类-动态管理';
    if (checksubmit('submit')) {
        if (!empty($_GPC['ids'])) {
            foreach ($_GPC['ids'] as $k => $v) {
                $data = array(
                    'title'        => safe_gpc_string($_GPC['title'][$k]),
                    'displayorder' => intval($_GPC['displayorder'][$k])
                );
                pdo_update('dgjy_article_category', $data, array('id' => intval($v)));
            }
            itoast('修改分类成功', referer(), 'success');
        }
    }

    $query     = load()->object('query');
    $categorys = $query->from('dgjy_article_category')->getall();//动态列表的分类
    include $this->template('news/news_category');
}

if ($op == 'category_add') {
    $_W['page']['title'] = '动态分类-动态管理';
    if (checksubmit('submit')) {
        $i = 0;
        if (!empty($_GPC['title'])) {
            foreach ($_GPC['title'] as $k => $v) {
                $title = safe_gpc_string($v);
                if (empty($title)) {
                    continue;
                }
                $data = array(
                    'title'        => $title,
                    'displayorder' => intval($_GPC['displayorder'][$k]),
                    'type'         => 'news',
                );
                pdo_insert('dgjy_article_category', $data);
                $i++;
            }
        }
        message('添加分类成功', $this->createWebUrl('news', array('op' => 'category')), 'success');
    }

    include $this->template('news/news_category');
}

if ($op == 'category_del') {
    $id = intval($_GPC['id']);
    pdo_delete('dgjy_article_category', array('id' => $id, 'type' => 'news'));
    pdo_delete('dgjy_article_news', array('cateid' => $id));
    itoast('删除分类成功', referer(), 'success');
}

if ($op == 'news_post') {
    $_W['page']['title'] = '编辑动态-动态列表';

    $query     = load()->object('query');
    $categorys = $query->from('dgjy_article_category')->getall();//动态列表的分类
    $id        = intval($_GPC['id']);
    $new       = $query->from('dgjy_article_news')->where('id', $id)->get();
//    var_dump($new);
    if (empty($new)) {
        $new = array(
            'is_display'   => 1,
            'is_show_home' => 0,
        );
    }

    if (checksubmit()) {
        $title   = trim($_GPC['title']) ? trim($_GPC['title']) : itoast('动态标题不能为空', '', 'error');
        $cateid  = intval($_GPC['cateid']) ? intval($_GPC['cateid']) : itoast('动态分类不能为空', '', 'error');
        $content = trim($_GPC['content']) ? trim($_GPC['content']) : itoast('动态内容不能为空', '', 'error');
        $data    = array(
            'title'        => $title,
            'cateid'       => $cateid,
            'content'      => safe_gpc_html(htmlspecialchars_decode($content)),
            'source'       => safe_gpc_string($_GPC['source']),
            'author'       => safe_gpc_string($_GPC['author']),
            'displayorder' => intval($_GPC['displayorder']),
            'click'        => intval($_GPC['click']),
            'is_display'   => intval($_GPC['is_display']),
            'is_show_home' => intval($_GPC['is_show_home']),
            'createtime'   => TIMESTAMP,
        );
        if (!empty($_GPC['thumb'])) {
            $data['thumb'] = $_GPC['thumb'];
        } elseif (!empty($_GPC['autolitpic'])) {
            $match = array();
            preg_match('/attachment\/(.*?)(\.gif|\.jpg|\.png|\.bmp)/', $data['content'], $match);
            if (!empty($match[1])) {
                $data['thumb'] = $match[1] . $match[2];
            }
        } else {
            $data['thumb'] = '';
        }

        if (!empty($new['id'])) {
            pdo_update('dgjy_article_news', $data, array('id' => $id));
        } else {
            pdo_insert('dgjy_article_news', $data);
        }
        message('编辑动态成功', url('site/entry/news', array('m' => 'dgjy', 'op' => 'list')), 'success');
    }

    include $this->template('news/news_add');
}

if ($op == 'news_delete') {
    $id = intval($_GPC['id']);
    pdo_delete('dgjy_article_news', array('id' => $id));
    itoast('删除动态成功', referer(), 'success');
}

if ($op == 'news_rec') {
    $id   = intval($_GPC['id']);
    $data = array(
        'is_show_home' => intval($_GPC['re'])
    );
    pdo_update('dgjy_article_news', $data, array('id' => $id));
    itoast('修改成功', referer(), 'success');
}

if ($op == 'news_show') {
    $id   = intval($_GPC['id']);
    $data = array(
        'is_display' => intval($_GPC['show'])
    );
    pdo_update('dgjy_article_news', $data, array('id' => $id));
    itoast('修改成功', referer(), 'success');
}

if ($op == 'news_detail') {
    $_W['page']['title'] = '动态详情-动态列表';

    $query = load()->object('query');
    $id    = intval($_GPC['id']);
    $news  = $query->from('dgjy_article_news')->where('id', $id)->get();
    if (is_error($news)) {
        itoast('动态不存在或已删除', referer(), 'error');
    }
    include $this->template('news/news_detail');
}
